package org.suyisen.feng.gateway.config;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Component;
import org.suyisen.feng.gateway.route.RouteService;
import org.suyisen.feng.gateway.utils.StringUtils;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 */
@Component
@RefreshScope
public class GatewayRouteInitConfig {
	
	private static final Logger log = LoggerFactory.getLogger(GatewayRouteInitConfig.class);
	
	@Autowired
	private GatewayRouteConfigProperties configProperties;
	
	@Autowired
	private NacosConfigProperties nacosConfigProperties;
	
	@Autowired
	private RouteService routeService;
	
	@Autowired
	private ConfigService configService;
	
	private final ObjectMapper objectMapper = new ObjectMapper();
	
	
	@PostConstruct
	public void init() {
		log.info("开始网关动态路由初始化...");
		try {
			String initConfigInfo = configService.getConfigAndSignListener(configProperties.getDataId(), configProperties.getGroup(), nacosConfigProperties.getTimeout(),new Listener() {

				@Override
				public Executor getExecutor() {
					return null;
				}

				@Override
				public void receiveConfigInfo(String configInfo) {
					if (StringUtils.isNotEmpty(configInfo)) {
						log.info("接收到网关路由更新配置：\r\n{}", configInfo);
						List<RouteDefinition> routeDefinitions = null;
                        try {
                            routeDefinitions = objectMapper.readValue(configInfo, new TypeReference<List<RouteDefinition>>() {
                            });
                        } catch (JsonProcessingException e) {
                            log.error("解析路由配置出错，" + e.getMessage(), e);
                        }
                        for (RouteDefinition definition : Objects.requireNonNull(routeDefinitions)) {
                            routeService.update(definition);
                        }
					}else {
						log.warn("当前网关无动态路由相关配置");
					}
				}
			});
			log.info("获取网关当前动态路由配置:\r\n{}", initConfigInfo);
            if (StringUtils.isNotEmpty(initConfigInfo)) {
                List<RouteDefinition> routeDefinitions = objectMapper.readValue(initConfigInfo, new TypeReference<List<RouteDefinition>>() {
                });
                for (RouteDefinition definition : routeDefinitions) {
                    routeService.add(definition);
                }
            } else {
                log.warn("当前网关无动态路由相关配置");
            }
            log.info("结束网关动态路由初始化...");
		} catch (Exception e) {
			log.error("初始化网关路由时发生错误", e);
		}
	}

}
